package com.example.demo.controller;



import com.example.demo.pojo.Anime;
import com.example.demo.repository.AnimeRepository;
import com.example.demo.service.AnimeService;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.data.domain.Pageable;


@Controller
public class AnimeController {

    @Autowired
    private AnimeService animeService;

    // 显示所有动漫
    @GetMapping("/anime")
    public String showAnimePage(Model model) {
        model.addAttribute("animeList", animeService.getAllAnime());
        return "anime"; // 返回 anime.html
    }

    // 显示单个动漫详情
    @GetMapping("/anime/{id}")
    public String showAnimeDetails(@PathVariable Long id, Model model) {
        Anime anime = animeService.getAnimeById(id);
        model.addAttribute("anime", anime);
        return "anime-details"; // 返回 anime-details.html
    }
    // 显示添加动漫页面
    @GetMapping("/add")
    public String showAddAnimePage(Model model) {
        model.addAttribute("anime", new Anime());
        return "add-anime"; // 返回 add-anime.html
    }

    // 处理添加动漫请求
    @PostMapping("/add")
    public String addAnime(@ModelAttribute Anime anime) {
        animeService.addAnime(anime);
        return "redirect:/anime"; // 添加成功后跳转到动漫列表页面
    }

    // 显示编辑动漫页面
    @GetMapping("/edit/{id}")
    public String showEditAnimePage(@PathVariable Long id, Model model) {
        Anime anime = animeService.getAnimeById(id);
        model.addAttribute("anime", anime);
        return "edit-anime"; // 返回 edit-anime.html
    }

    // 处理编辑动漫请求
    @PostMapping("/edit/{id}")
    public String updateAnime(@PathVariable Long id, @ModelAttribute Anime anime) {
        animeService.updateAnime(id, anime);
        return "redirect:/anime"; // 更新成功后跳转到动漫列表页面
    }

    // 处理删除动漫请求
    @GetMapping("/delete/{id}")
    public String deleteAnime(@PathVariable Long id) {
        animeService.deleteAnime(id);
        return "redirect:/anime"; // 删除成功后跳转到动漫列表页面
    }



}